package com.yikes.srb.controller.web;

import com.yikes.srb.common.result.PageResult;
import com.yikes.srb.common.result.Result;
import com.yikes.srb.leadingModle.form.dict.DictAddForm;
import com.yikes.srb.leadingModle.form.dict.DictEditForm;
import com.yikes.srb.leadingModle.form.dict.DictQuery;
import com.yikes.srb.leadingModle.vo.DictVO;
import com.yikes.srb.service.DictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * <p>
 * 数据字典 前端控制器
 * </p>
 *
 * @author guanrong.yin
 * @since 2024-12-03
 */
@RequiredArgsConstructor
@Tag(name = "DictController", description = "数据字典")
@RestController
@RequestMapping("/admin/dict")
public class DictController {

    private final DictService service;

    @Operation(description = "分页查询")
    @GetMapping("/list_page")
    public Result<PageResult<DictVO>> listPage(DictQuery query) {

        return Result.ok(service.listPage(query));
    }

    @Operation(description = "根据id查询单条信息")
    @GetMapping("/info/{id}")
    public Result<DictVO> infoId(@PathVariable("id") Long id) {

        return Result.ok(service.infoId(id));
    }

    @Operation(description = "新增")
    @PostMapping("/create")
    public Result create(@Valid @RequestBody DictAddForm form) {

        service.create(form);
        return Result.ok();
    }

    @Operation(description = "编辑")
    @PutMapping("/edit")
    public Result edit(@Valid @RequestBody DictEditForm form) {

        service.edit(form);
        return Result.ok();
    }

    @Operation(description = "删除")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Long id) {

        service.del(id);
        return Result.ok();
    }

    @Operation(description = "导入excel文件数据")
    @PostMapping("/import_excel")
    public Result importExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request) {

        service.importExcel(file);
        return Result.ok();
    }

    @Operation(description = "导出excel文件数据")
    @GetMapping("/export_excel")
    public Result exportExcel(DictQuery query, HttpServletRequest request, HttpServletResponse response) {

        service.exportExcel(query, request, response);
        return Result.ok();
    }

    @Operation(description = "根据上级id获取子节点数据列表")
    @GetMapping("/listByParentId/{parentId}")
    public Result listByParentId(@PathVariable Long parentId) {

        List<DictVO> dictList = service.listByParentId(parentId);
        return Result.ok(dictList);
    }

    @Operation(description = "三级树列表数据")
    @GetMapping("/tree")
    public Result tree() {

        return Result.ok(service.tree());
    }

    @Operation(description = "根据dictCode获取下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode) {

        return Result.ok(service.findByDictCode(dictCode));
    }

}
